<?php
require_once($GLOBALS['LNG'].$_SESSION[$SESSLG].'/lang.default.mex');
if (!isset($_POST['no_prom'])) {
    $_POST['no_prom'] = 0;
}
if (isset($_POST['CID'])) {
    $OBJCTCL = $_POST['CID'];
} elseif (isset($_POST['OBJCTCL'])) {
    $OBJCTCL = $_POST['OBJCTCL'];
}
if (!isset($MDOR)) {
    $MDOR = 'ED';
}
if ($_POST['FraCalc'] > 0) {
    $Proms = '';
    $prom_active = 0;
    $Inv['FraProm'] = 0;
    $Inv['FraTaxProm'] = 0;
    $prom_bonuscalc = 0;
?>
    <table width="100%"  border="0" cellpadding="1" cellspacing="1" bgcolor="#666666">
        <tr>
            <td bgcolor="#FFFFFF">
                <table width="100%"  border="0" cellpadding="1" cellspacing="0" bgcolor="#666666">
                    <tr bgcolor="#A76540" height="20">
                        <td>
                            Promocao
<?php mn_field('button', $MDOR, 'AddProm', $lang['XFK0WK2A']['add_prom'], '', '', '', '" disabled readonly="'); ?></td>
                    <td><?php echo $lang['basic']['start']; ?></td>
                    <td><?php echo $lang['basic']['end']; ?></td>
                    <td>Pontos Necessários</td>
                    <td><?php echo $lang['basic']['amount']; ?></td>
                    <td></td>
                </tr>
                <?php
//if ($_POST['prom_id'] == 0) {
                $PROM = $GLOBALS['MYT']->db_array("SELECT
			REF,
			CliNumero,
			Prom_Description,
			Prom_Start,
			Prom_End,
			PromField,BonusCost,
			Prom_promValue,
			IF(
                           (CliLastSend IS NULL OR DATE(CliLastSend) >= Prom_promTimeEnd)
                                AND
                            (CliLastSend IS NULL OR DATE(CliLastSend) < Prom_promTime),
                            1,
                            0
                        ) AS PromValid,
			DATE(CliFechaAlta) AS DataCliente
		FROM promotions
			INNER JOIN beneficiarios ON BnfNumero = {$_POST['OBJCTRC']}
			INNER JOIN bonusaccounts ON bac_Client = {$OBJCTCL} AND bac_Balance >= BonusCost
			INNER JOIN clientes ON CliNumero = bac_Client
			LEFT OUTER JOIN clientes_ext ON CliNumero = CliExtNumero
		WHERE PromStatus IN (2,3)
			AND (Prom_Domain IS NULL OR Prom_Domain = {$_SESSION[$SESSCD]})
			AND Prom_Country = BnfPais
			AND PromValue <= {$_POST['FraCalc']}
			AND '" . date('Y-m-d') . "' BETWEEN Prom_Start AND Prom_End
			AND AutoAttrib = 1
			GROUP BY REF HAVING PromValid = 1
			ORDER BY Prom_promValue DESC
			LIMIT 0,1");
                /* } else {
                  $PROM = $GLOBALS['MYT']->db_array("SELECT
                  REF,
                  Prom_Description,
                  Prom_Start,
                  Prom_End,
                  PromField,BonusCost,
                  Prom_promValue,
                  IF(DATE(MAX(FraFechaIngreso)) < Prom_promTime,1,0) AS PromValid,�
                  DATE(CliFechaAlta) AS DataCliente,
                  COUNT(FraID) AS NumSend
                  FROM promotions
                  INNER JOIN beneficiarios ON BnfNumero = {$_POST['OBJCTRC']}
                  INNER JOIN bonusaccounts ON bac_Client = {$OBJCTCL} AND bac_Balance >= BonusCost
                  INNER JOIN clientes ON CliNumero = bac_Client
                  LEFT OUTER JOIN facturas ON FraCliNumero = {$OBJCTCL} AND FraStatus NOT IN (1,9,10)
                  WHERE REF = {$_POST['prom_id']}
                  GROUP BY REF HAVING PromValid = 1 OR NumSend = 0");
                  } */
                $TProms = $PROM[0];
                if ($_POST['no_prom'] == 1) {
                    $TProms = 0;
                }
                for ($pr = 1; $pr <= $TProms; $pr++) {
                    $prom_active = 1;
                    echo '<tr height="20" bgcolor="' . gridlines($pr) . '">
              <td>' . $PROM[$pr]['Prom_Description'] . '</td>
              <td>' . $PROM[$pr]['Prom_Start'] . '</td>
              <td>' . $PROM[$pr]['Prom_End'] . '</td>
             <td>' . $PROM[$pr]['BonusCost'] . '</td>
             <td>' . $PROM[$pr]['Prom_promValue'];

                    //$Proms .= $PROM[$pr]['REF'];
                    $PromField = $PROM[$pr]['PromField'];
                    $Inv[$PromField] += $PROM[$pr]['Prom_promValue'];
                    //if ($pr < $TProms) { $Proms .= ','; }
                ?>
                </td>
                <td>
                 <?php $prom_bonuscalc = $PROM[$pr]['BonusCost']; ?>
               <img src="<?php echo $GLOBALS['GFX']; ?>dele.png" alt="Remover Promo&ccedil;&atilde;o" onclick="
                   document.InvoiceProperties.no_prom.value=1;
                   document.InvoiceProperties.prom_bonus.value=0;
                   document.InvoiceProperties.action='_response/call.php?get=Operative/remittancecalc';
                                subform('InvoiceProperties','inv_Result');"></td>
                    </tr>
<?php
                }
                if ($prom_active == 0) {
                    echo '<tr height="20" bgcolor="#FFFFFF"><td colspan="7">' . $lang['XFK0WK2A']['no_prom'] . '</td></tr>';
                } ?>
                </table></td>
                </tr>
                </table>
                <script charset="utf-8" type="text/javascript">document.InvoiceProperties.prom_bonus.value=<?php echo $prom_bonuscalc; ?>;</script>
<?php
            }